﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Linq;
using System.Security.Claims;

namespace Topro.Web.Controllers
{
    /// <summary>
    /// 基础控制
    /// </summary>
    public class BaseController : Controller
    {

        /// <summary>
        /// 用户编号
        /// </summary>
        public int UserId;

        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName;

        /// <summary>
        /// Ip
        /// </summary>
        public string Ip;

        /// <summary>
        /// OnActionExecuting
        /// </summary>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var isAjaxRequest = context.HttpContext.Request.Headers["x-requested-with"] == "XMLHttpRequest";
            //获取ip地址
            this.Ip = context.HttpContext.Connection.RemoteIpAddress.ToString().Replace("::ffff:", "");

            //if (HttpContext.User.Claims != null && HttpContext.User.Identity.IsAuthenticated && HttpContext.User.Claims.Count() == 2)
            //{
            //    this.UserId = int.Parse(HttpContext.User.Claims.First().Value);
            //    this.UserName = HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
            //}
            //else
            //{
            //    if (isAjaxRequest)
            //    {
            //        context.Result = Json(new
            //        {
            //            IsLogout = true,
            //            Message = "登录超时，请重新登录",
            //        });
            //    }
            //    else
            //    {
            //        context.Result = new ViewResult { ViewName = "~/Views/Account/Index.cshtml" };
            //    }
            //}

            base.OnActionExecuting(context);
        }
    }
}
